package com.reactim.user.controller;

import com.reactim.common.response.Result;
import com.reactim.user.dto.UserLoginRequest;
import com.reactim.user.dto.UserRegisterRequest;
import com.reactim.user.dto.UserResponse;
import com.reactim.user.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 用户控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Mono<Result<UserResponse>> register(@Valid @RequestBody UserRegisterRequest request) {
        return userService.register(request)
                .map(Result::success)
                .onErrorResume(e -> {
                    log.error("用户注册失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Mono<Result<String>> login(@Valid @RequestBody UserLoginRequest request) {
        return userService.login(request)
                .map(token -> Result.success("登录成功", token))
                .onErrorResume(e -> {
                    log.error("用户登录失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
    
    /**
     * 获取用户信息
     */
    @GetMapping("/{userId}")
    public Mono<Result<UserResponse>> getUserById(@PathVariable Long userId) {
        return userService.getUserById(userId)
                .map(Result::success)
                .onErrorResume(e -> {
                    log.error("获取用户信息失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
    
    /**
     * 根据用户名获取用户信息
     */
    @GetMapping("/username/{username}")
    public Mono<Result<UserResponse>> getUserByUsername(@PathVariable String username) {
        return userService.getUserByUsername(username)
                .map(Result::success)
                .onErrorResume(e -> {
                    log.error("获取用户信息失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/{userId}")
    public Mono<Result<UserResponse>> updateUser(@PathVariable Long userId, 
                                                @RequestBody UserResponse userInfo) {
        return userService.updateUser(userId, userInfo)
                .map(user -> Result.success("更新成功", user))
                .onErrorResume(e -> {
                    log.error("更新用户信息失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
    
    /**
     * 搜索用户
     */
    @GetMapping("/search")
    public Mono<Result<List<UserResponse>>> searchUsers(@RequestParam String keyword) {
        return userService.searchUsers(keyword)
                .map(Result::success)
                .onErrorResume(e -> {
                    log.error("搜索用户失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
    
    /**
     * 检查用户名是否存在
     */
    @GetMapping("/exists/{username}")
    public Mono<Result<Boolean>> existsByUsername(@PathVariable String username) {
        return userService.existsByUsername(username)
                .map(Result::success)
                .onErrorResume(e -> {
                    log.error("检查用户名失败", e);
                    return Mono.just(Result.error(e.getMessage()));
                });
    }
}